package com.authine.cloudpivot.web.api.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author chenjq
 */
public final class Util {

    private Util() {}

    public static String object2String(Object object) {
        if (object == null) {
            return null;
        } else {
            if ("".equals(object.toString().trim())) {
                return null;
            }
            return object.toString().trim();
        }
    }

    public static String object2StringNotNull(Object object) {
        if (object == null) {
            return "";
        } else {
            if ("".equals(object.toString().trim())) {
                return object.toString().trim();
            }
            return object.toString().trim();
        }
    }

    public static Double object2Double(Object object) {
        if (object == null) {
            return null;
        } else {
            return (null == object2String(object)) ? null : Double.parseDouble(object2String(object));
        }
    }

    public static Integer object2Integer(Object o) {
        if (o == null) {
            return null;
        } else {
            return (null == object2String(o)) ? null : Integer.parseInt((object2String(o).split("\\."))[0]);
        }
    }

    public static Long object2Long(Object o) {
        if (o == null) {
            return null;
        } else {
            return (null == object2String(o)) ? null : Long.parseLong((object2String(o).split("\\."))[0]);
        }
    }

    public static String null2String(Integer o) {
        if (o == null) {
            return "";
        } else {
            return o + "";
        }
    }

    public static String null2String(Double o) {
        if (o == null) {
            return "";
        } else {
            return o + "";
        }
    }

    public static String null2String(String o) {
        if (o == null) {
            return "";
        } else if ("null".equals(o)) {
            return "";
        } else {
            return o + "";
        }
    }

    /**
     * 将字符串首字母变大写
     * 
     * @param str
     *            字符串
     * @return
     */
    public static String upperCaseFirstChar(String str) {
        str = str.substring(0, 1).toUpperCase() + str.substring(1);
        return str;

    }

    /**
     * 将字符串首字母变小写
     * 
     * @param str
     *            字符串
     * @return
     */
    public static String lowerCaseFirstChar(String str) {
        str = str.substring(0, 1).toLowerCase() + str.substring(1);
        return str;

    }

    /**
     * 检查数据
     * 
     * @param dataElement
     *            sap返回数据
     * @return
     */
    // public static DataElement checkDataElement(DataElement dataElement){
    // if(dataElement == null){
    // dataElement = new DataElement();
    // }
    // return dataElement;
    // }

    /**
     * 将列表转成字符串，中间用逗号隔开
     * 
     * @return
     */
    public static String listToString(List<Object> list) {
        StringBuilder stringBuilder = new StringBuilder();
        if (list != null && list.size() > 0) {
            stringBuilder.append(list.get(0).toString());
            for (int i = 1; i < list.size(); i++) {
                stringBuilder.append(",");
                stringBuilder.append(list.get(i).toString());
            }
            return stringBuilder.toString();
        } else {
            return "";
        }
    }

    /**
     * 将列表转成字符串，中间用逗号隔开
     * 
     * @return
     */
    public static String stringListToString(List<String> list) {
        StringBuilder stringBuilder = new StringBuilder();
        if (list != null && list.size() > 0) {
            stringBuilder.append(list.get(0));
            for (int i = 1; i < list.size(); i++) {
                stringBuilder.append(",");
                stringBuilder.append(list.get(i).toString());
            }
            return stringBuilder.toString();
        } else {
            return "";
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        System.out.println(" remove duplicate " + list);
    }

    public static Boolean checkObjectIsNull(Object object) {
        if (object == null || "".equals(object.toString().trim())) {
            return true;
        }
        return false;
    }

    /**
     * 数据转换，主要对boolean进行了转换
     * 
     * @param obj
     * @return
     */
    // public static Object dataTransform(Object obj) {
    // if(obj ==null){
    // return "";
    // }
    //
    // if (obj instanceof Boolean) {
    // boolean value = (Boolean) obj;
    // if (value) {
    // return "是";
    // } else {
    // return "否";
    // }
    // }else if(obj instanceof ZonedDateTime){
    // return DateUtil.ZonedDateTimeToStr((ZonedDateTime) obj);
    // }
    // return obj;
    // }
}
